package com.gxh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxh.context.BaseContext;
import com.gxh.dto.FavoriteDTO;
import com.gxh.dto.FavoritePageQueryDTO;
import com.gxh.entity.Favorite;
import com.gxh.exception.BaseException;
import com.gxh.mapper.FavoriteMapper;
import com.gxh.mapper.MachineMapper;
import com.gxh.result.PageResult;
import com.gxh.service.FavoriteService;
import com.gxh.vo.FavoriteVO;
import com.gxh.vo.MachineItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;
    @Autowired
    private MachineMapper  machineMapper;

    /**
     * 用户收藏
     * @param favoriteDTO
     */
    public void save(FavoriteDTO favoriteDTO) {
        Long  userId = BaseContext.getCurrentId();
        Long machineId = favoriteDTO.getMachineId();
        QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("machine_id",machineId);
        Favorite favorite = favoriteMapper.selectOne(queryWrapper);
        //判断当前用户是否已经收藏过该设备
        if (favorite != null){
            throw new BaseException("当前用户已经收藏过该设备");
        }
        favorite = new Favorite();
        favorite.setUserId(userId);
        favorite.setMachineId(machineId);
        favorite.setCreateTime(LocalDateTime.now());
        favoriteMapper.insert(favorite);
    }

    /**
     * 用户收藏列表
     * @return
     */
    public List<MachineItemVO> list() {
        Long  userId = BaseContext.getCurrentId();
        //获取到用户收藏列表
        List<Favorite> favorites = favoriteMapper.selectList(new QueryWrapper<Favorite>().eq("user_id",userId));
        //提取出设备id
        List<Long> machineIds = favorites.stream().map(Favorite::getMachineId).toList();
        if(machineIds.isEmpty()){
            return Collections.emptyList();
        }
        //根据设备id查询设备信息
        List<MachineItemVO> machineItemVOS = machineMapper.getByIds(machineIds);
        return machineItemVOS;
    }


    public Integer count() {
        Long  userId = BaseContext.getCurrentId();
        QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return Math.toIntExact(favoriteMapper.selectCount(queryWrapper));

    }

    /**
     * 用户取消收藏
     * @param id
     */
    public void delete(Long id) {
        Long  userId = BaseContext.getCurrentId();
        QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("machine_id",id);
        favoriteMapper.delete(queryWrapper);
    }


    /**
     * 分页查询用户收藏
     * @param favoritePageQueryDTO
     * @return
     */
    public PageResult pageQuery(FavoritePageQueryDTO favoritePageQueryDTO) {
        PageHelper.startPage(favoritePageQueryDTO.getPage(), favoritePageQueryDTO.getPageSize());
        Page<FavoriteVO> page = favoriteMapper.pageQuery(favoritePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }
}
